#
# SetupDialog.py
# (c) 2008 Will Baker
#

from PyQt4.QtCore import QObject, SIGNAL, SLOT
from PyQt4.QtGui import QDialog, QLabel, QLineEdit, QPushButton, QHBoxLayout, QVBoxLayout
from PyQt4.QtGui import QFileDialog

class SetupDialog(QDialog):
    """
    Dialog through which essential options can be set up. This is invoked when any of
    the options are missing.
    """
    def __init__( self, library_path, plugins_path, parent=None ):
        QDialog.__init__( self, parent )
        
        self.library_path = library_path
        self.plugins_path = plugins_path
        
        # Create widgets
        info_label = QLabel( "As part of the initial OpenTome setup stage you need to provide\n"+
                             "some paths. The library path is where OpenTome will search for\n" +
                             "content to load, and the plugins path is where OpenTome will\n" +
                             "search for plugins to use when loading this content.\n\n",
                             self )
        
        library_path_label = QLabel( "Library Path", self )
        self.library_path_edit = QLineEdit( self )
        self.library_path_edit.setText( library_path )
        library_path_browse = QPushButton( "...", self )
        library_path_browse.setMaximumWidth( 30 )
        
        plugins_path_label = QLabel( "Plugins Path", self )
        self.plugins_path_edit = QLineEdit( self )
        self.plugins_path_edit.setText( plugins_path )
        plugins_path_browse = QPushButton( "...", self )
        plugins_path_browse.setMaximumWidth( 30 )
        
        ok_button = QPushButton( "Ok", self )
        cancel_button = QPushButton( "Cancel", self )
        
        # Set up widget layout
        top_layout = QVBoxLayout()
        
        top_layout.addWidget( info_label )
        
        hbox_layout = QHBoxLayout()
        hbox_layout.addWidget( library_path_label )
        hbox_layout.addWidget( self.library_path_edit )
        hbox_layout.addWidget( library_path_browse )
        top_layout.addLayout( hbox_layout )
        
        hbox_layout = QHBoxLayout()
        hbox_layout.addWidget( plugins_path_label )
        hbox_layout.addWidget( self.plugins_path_edit )
        hbox_layout.addWidget( plugins_path_browse )
        top_layout.addLayout( hbox_layout )
        
        hbox_layout = QHBoxLayout()
        hbox_layout.addStretch()
        hbox_layout.addWidget( ok_button )
        hbox_layout.addWidget( cancel_button )
        top_layout.addLayout( hbox_layout )
        
        self.setLayout( top_layout )
        
        # Set up connections
        QObject.connect( library_path_browse, SIGNAL("clicked()"),  self.on_browse_library_path )
        QObject.connect( plugins_path_browse, SIGNAL("clicked()"),  self.on_browse_plugins_path )
        QObject.connect( ok_button,           SIGNAL("clicked()"),  self, SLOT("accept()") )
        QObject.connect( cancel_button,       SIGNAL("clicked()"),  self, SLOT("reject()") )
        QObject.connect( self,                SIGNAL("accepted()"), self.on_accept )
        
        self.setWindowTitle( "Initial Setup" )

    def on_browse_library_path( self ):
        path = QFileDialog.getExistingDirectory( self, "Select Directory", self.library_path_edit.text() )
        self.library_path_edit.setText( path )
        
    def on_browse_plugins_path( self ):
        path = QFileDialog.getExistingDirectory( self, "Select Directory", self.plugins_path_edit.text() )
        self.plugins_path_edit.setText( path )
        
    def on_accept( self ):
        self.library_path = str(self.library_path_edit.text())
        
        if self.library_path == "":
            self.library_path = "."
        
        self.plugins_path = str(self.plugins_path_edit.text())
